package com.ken.autocode.tools.generator.util;

import com.ken.autocode.tools.generator.config.ConfigInfo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyFile {
    private String topLevel = "";
    private String modelName = "";
    private String proSite = "";
    private String daoFolder = "";
    private String serviceImplFoledr = "";
    private String serviceInteFoledr = "";
    private String webFoledr = "";

    // 建立目录
    public MyFile(String daoFolder,String serviceImplFoledr,String serviceInteFoledr,String webFoledr,String topLevel,String modelName,String proSite) {
        this.daoFolder = daoFolder;
        this.serviceImplFoledr = serviceImplFoledr;
        this.serviceInteFoledr = serviceInteFoledr;
        this.webFoledr = webFoledr;
        this.topLevel = topLevel;
        this.modelName = modelName;
        this.proSite = proSite;
    }

    /**
     * 写Java文件
     * @param content 文件内容
     * @param fileName 文件名称
     * @param fileSufix 文件后缀名称 如 Mapper.xml 和 _list.jsp
     * @param entityTpl 包名
     */
    public void writeFile(String content, String fileName, String fileSufix, EntityTpl entityTpl) {
        try {
            String packageName =null;
            if(".java".equals(fileSufix)){
                packageName = entityTpl.getEntityPackage();
            }else if("Mapper.java".equals(fileSufix)){
                packageName = entityTpl.getMapperInterfacePackage();
            }else if("Mapper.xml".equals(fileSufix)){
                packageName = entityTpl.getMapperXmlPackage();
            }else if("Service.java".equals(fileSufix)){
                packageName = entityTpl.getServicePackage();
            }else if("ServiceImpl.java".equals(fileSufix)){
                packageName = entityTpl.getServiceImplPackage();
            }else if("Controller.java".equals(fileSufix)){
                packageName = entityTpl.getControllerPackage();
            }else if(fileSufix.contains("_list") || fileSufix.contains("_update") || fileSufix.contains("_Other")){
                packageName = entityTpl.getViewPackage();
            }

            packageName = packageName.replace(".", "\\");

            Pattern pattern =Pattern.compile(".jsp|.jspf|.jspx|.html|.ftl|.js|.txt");
            Matcher matcher = pattern.matcher(fileSufix);

            //是页面文件?
            String rootFolder = matcher.find()?ConfigInfo.VIEWFOLDER :ConfigInfo.JAVAFODER;

            String folder = proSite + rootFolder + packageName;
            createFolder(folder);
            fileName = folder +"\\" + fileName + fileSufix;

            // 生成文件
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
            osw.write(content);

            System.out.println(fileName+"已生成成功\n");
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void createFolder(String folderName) {
        File moduleFile = new File(folderName);
        //如果文件路径不存在
        if (!moduleFile.exists()) {
            moduleFile.mkdirs();
        }
    }
}
